<template>
  <div>
    <!-- 搜索区域 -->
    <a-form layout="horizontal">
      <a-row >
        <div>
          <a-col :md="12" :sm="24" >
            <a-form-item
              label="图书名称"
              :labelCol="{span: 4}"
              :wrapperCol="{span: 18, offset: 2}">
              <a-input v-model="itemName"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24" >
            <a-form-item
              label=""
              :labelCol="{span: 4}"
              :wrapperCol="{span: 18, offset: 1}">
                <span style="padding-top: 3px;margin-left: 20px;">
                  <a-button type="primary" @click="listData">查询</a-button>
                  <a-button style="margin-left: 8px" @click="itemName = ''">重置</a-button>
                </span>
            </a-form-item>
          </a-col>
        </div>
      </a-row>
    </a-form>
    <a-table ref="TableInfo"
             :columns="columns"
             :dataSource="data"
             :pagination="pagination"
             :loading="loading"
             @change="handleTableChange"
    >
      <template slot="image" slot-scope="text, record">
        <img :src="record.image" style="width: 50px;">
      </template>
      <template slot="isLike" slot-scope="text, record">
        <a v-if="record.isLike" @click="cancelItem(record)">取消收藏</a>
        <a v-else @click="collectItem(record)">收藏</a>
      </template>
    </a-table>
  </div>
</template>

<script>
export default {
  data () {
    return {
      data: [],
      itemName: '',
      searchList: [],
      resultList: [],
      foodList: [],
      currentKey: '',
      loading: false,
      systemUserId: this.$store.state.account.user.userId,
      columns: [
        {title: '图书编号', dataIndex: 'itemid', ellipsis: true},
        {title: '图书名称', dataIndex: 'itemName', ellipsis: true},
        {title: '备注', dataIndex: 'remark'},
        {
          title: '图片', dataIndex: 'image',
          scopedSlots: {customRender: 'image'}
        },
        {title: '相似度', dataIndex: 'similar'},
        {title: '情况', dataIndex: 'status'},
        {title: '操作', dataIndex: 'isLike', scopedSlots: {customRender: 'isLike'}}
      ],
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      }
    }
  },
  methods: {
    reset () {
    },
    collectItem (record) {
      this.$post('item/addLike', {
        userId: this.systemUserId,
        itemId: record.itemid
      }).then((r) => {
        this.listData(this.currentKey)
      }).catch((r) => {

      })
    },
    cancelItem (record) {
      this.$delete('item/deleteUserLike', {
        userId: this.systemUserId,
        itemId: record.itemid
      }).then((r) => {
        this.listData(this.currentKey)
      }).catch((r) => {

      })
    },
    handleSearch (value) {
      // this.listFoot(value)
      this.resultList = []
      for (let bean of this.searchList) {
        if (bean.itemName.indexOf(value) !== -1) {
          this.resultList.push(bean)
        }
      }
    },
    handleTableChange (pagination) {
      this.paginationInfo = pagination
      this.listData(this.currentKey)
    },
    handleChange (value, option) {
      this.itemName = value
      console.log(option)
      this.currentKey = option.key
      this.listData(this.currentKey)
      // this.listFoot(value)
    },
    listFoot () {
      this.$get(`item/itemAll`).then((r) => {
        this.searchList = r.data
        console.log(r)
      })
    },
    listData () {
      let params = {}
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$get('item/user/like', {
        ...params,
        systemUserId: this.systemUserId,
        itemName: this.itemName
      }).then((r) => {
        console.log(r)
        this.data = r.data.rows
        const pagination = {...this.pagination}
        pagination.total = r.data.total
        this.pagination = pagination
        // 数据加载完毕，关闭loading
        this.loading = false
      })
    }
  },
  mounted () {
    this.listData()
  }
}
</script>

<style scoped>

</style>
